package com.wzy.oa_sys.controller.schedule;


import com.wzy.oa_sys.model.request.schedule.ScheduleReq;
import com.wzy.oa_sys.model.request.schedule.UseBoardroomReq;
import com.wzy.oa_sys.model.response.notice.NoticeResp;
import com.wzy.oa_sys.model.response.schedule.ScheduleResp;
import com.wzy.oa_sys.model.response.schedule.UseBoardroomDataResp;
import com.wzy.oa_sys.model.response.schedule.UseBoardroomResp;
import com.wzy.oa_sys.service.schedule.ScheduleService;
import com.wzy.oa_sys.service.schedule.UseBoardroomService;
import com.wzy.oa_sys.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private UseBoardroomService useBoardroomService;

    @GetMapping("/list")
    public List<ScheduleResp> getScheduleList() {
        return scheduleService.getScheduleList();
    }

    @GetMapping("/getMyScheduleList")
    public List<ScheduleResp> getMyScheduleList(HttpServletRequest request){
        long myId = jwtUtil.extractUserId(request);
        return scheduleService.getMyScheduleList(myId);
    }

    @PostMapping("/addSchedule")
    public void addSchedule(@RequestBody ScheduleReq scheduleReq, HttpServletRequest request){
        long l = jwtUtil.extractUserId(request);
        System.out.println("当前登录用户id为："+l);
        System.out.println("新增的日程为："+scheduleReq.toString());
        scheduleService.addSchedule(scheduleReq,l);
    }

    @DeleteMapping("/deleteScheduleById/{id}")
    public void deleteScheduleById(@PathVariable("id") Integer id){
        System.out.println("要删除的日程id为："+id);
        int i =scheduleService.deleteScheduleById(id);
        System.out.println("删除的结果为："+i);
    }

    @GetMapping("/getScheduleDetailById/{id}")
    public ScheduleResp getScheduleDetailById(@PathVariable("id") Integer id){
        return scheduleService.getScheduleDetailById(id);
    }

    @PostMapping("/checkBoardroomSchedule")
    public UseBoardroomResp checkBoardroomSchedule(
            @RequestBody UseBoardroomReq useBoardroomReq,HttpServletRequest request) {
        useBoardroomReq.setStarttime(useBoardroomReq.getStarttime().plusHours(8));
        useBoardroomReq.setDeadline(useBoardroomReq.getDeadline().plusHours(8));
        System.out.println("开始检查会议室使用情况"+useBoardroomReq);
        boolean isOccupied = useBoardroomService.checkBoardroomOccupied(useBoardroomReq);

        return UseBoardroomResp.builder().isOccupied(isOccupied).build();
    }

    @GetMapping("/getBoardroomSchedules")
    public List<UseBoardroomDataResp> getBoardroomSchedules(){
        List<UseBoardroomDataResp> boardroomSchedules = useBoardroomService.getBoardroomSchedules();
        System.out.println("会议室使用情况"+boardroomSchedules);
        return boardroomSchedules;
    }

    @PutMapping("/updateSchedule")
    public void updateSchedule(@RequestBody ScheduleReq scheduleReq, HttpServletRequest request){
        Long userId = jwtUtil.extractUserId(request);
        scheduleService.updateSchedule(scheduleReq,userId);
    }
    @PostMapping("/reminder/{id}")
    public void reminder(@PathVariable("id") Integer id, HttpServletRequest request){
        scheduleService.reminder(id,request);
    }

}
